package com.rimi.cs.javaweb.controller;

import com.rimi.cs.javaweb.pojo.User;
import com.rimi.cs.javaweb.service.ICartService;
import com.rimi.cs.javaweb.service.IUserService;
import com.rimi.cs.javaweb.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录注册控制器
 *
 * @author Hayes
 * @date 2018-06-27 13:06
 */
@Controller
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private ICartService cartService;

    /**
     * 登录
     *
     * @param userName 用户名
     * @param userPassword 密码
     * @return 成功或者失败页面
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(String userName, String userPassword, Model model, HttpServletRequest request) {
        // 调用登录操作
        boolean isLogin = userService.login(userName, userPassword, request);
        // 如果登录成功
        if (isLogin) {
            User user = (User) request.getSession().getAttribute(Constant.USER_INFO);
            int cartItemCount = cartService.cartItemCount(request);
            request.getSession().setAttribute("cartItemCount",cartItemCount);
            return "index";
        } else {
            model.addAttribute("error", "登录失败，请重新登录");
            return "account";
        }
    }

    /**
     * 用户注册功能
     * @param name 用户ID
     * @param email 邮箱地址
     * @param phone 手机号码
     * @param gender 性别
     * @param password 用户密码
     * @return
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String register(String name, String email, String phone, String gender, String password) {
        User user = new User();
        user.setUsername(name);
        user.setEmail(email);
        user.setPhone(phone);
        user.setGender((gender).equals("male")? 0 : 1);
        user.setPassword(password);

        boolean isRegister = userService.register(user);

        return isRegister ? "index" : "404" ;
    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    @RequestMapping(value = "/exit",method = RequestMethod.GET)
    public String exit(HttpServletRequest request){
        request.getSession().invalidate();
        return "index";
    }


    /**
     * 查找邮箱是否存在
     *
     * @param email
     * @param request
     * @return
     */
    /*@ResponseBody
    @RequestMapping(value = "/mailExist", method = RequestMethod.GET)
    public Map mailExist(String email, HttpServletRequest request){
        Map<String, Boolean> exist = new HashMap<>();
        boolean mailExists = userService.judgeEmail(email, request);
        exist.put("mailExists", mailExists);
        return exist;
    }*/

    /**
     * 重置密码
     *
     * @param password
     * @param request
     * @return
     */
    @RequestMapping(value = "/reset", method = RequestMethod.POST)
    public String reset(@RequestParam("password") String password, HttpServletRequest request){
        userService.updatePassword(password, request);

        return "/index";
    }


    /**
     * 判断用户是否存在
     * @param userName
     * @return
     */
    @RequestMapping(value = "/ifUserNameExist",method = RequestMethod.GET)
    @ResponseBody
    public Map ifExistUserName(String userName){
        boolean existUserName = userService.existUserName(userName);
        Map<String,Boolean> map = new HashMap();
        map.put("exist",existUserName);
        return map;
    }

    /**
     * 判断用户邮箱是否存在
     * @param email
     * @return
     */
    @RequestMapping(value = "/ifEmailExist",method = RequestMethod.GET)
    @ResponseBody
    public Map ifExistEmail(String email){
        boolean existEmail = userService.existEmail(email);
        Map<String,Boolean> map = new HashMap();
        map.put("exist",existEmail);
        return map;
    }
}
